import React from 'react'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import { Radio, Empty, message } from 'antd'
import moment from 'moment'
import style from './CourtList.module.less'

class CourtList extends Component {
  state = {
    todayCourtList: [],
    nextDayCourtList: [],
    nextDayTitle: '明天',
    activeCourtTag: '今天'
  }

  // 格式化仲裁员
  getArbitrator (li) {
    const list = [li.arbitrator]
    if (li.arbitrator1) {
      list.push(li.arbitrator1)
      list.push(li.arbitrator2)
    }
    return list.join('、')
  }

  // 格式化时间
  getTime (d) {
    if (!d) {
      return d
    }
    return moment(d * 1000).format('HH:mm')
  }

  // 获取开庭信息
  getCourtListByDay (day) {
    return this.$get('/api/xapc/hearingroombook/day', {
      day
    }).then(res => {
      if (res.status === 1) {
        return res.data
      } else {
        message.warning(res.message)
      }
    })
  }

  // 获取开庭信息
  getCourt () {
    const day = moment()
    const dayList = [day.format('YYYY-MM-DD')]
    const dayTitles = ['今天', '明天']
    day.add(1, 'd')
    dayList.push(day.format('YYYY-MM-DD'))
    if (day.day() === 6) {
      day.add(1, 'd')
      dayList.push(day.format('YYYY-MM-DD'))
      day.add(1, 'd')
      dayList.push(day.format('YYYY-MM-DD'))
      dayTitles.push('周日')
      dayTitles.push('下周一')
    }
    const hide = message.loading('获取开庭信息中', 120)
    Promise.all(dayList.map(d => this.getCourtListByDay(d)))
      .then(res => {
        hide()
        this.setState({
          todayCourtList: res[0]
        })
        for (let i = 1; i < res.length; i++) {
          if (res[i].length > 0) {
            this.setState({
              nextDayCourtList: res[i],
              nextDayTitle: dayTitles[i]
            })
            break
          }
        }
        if (this.state.nextDayCourtList.length === 0) {
          this.setState({
            nextDayTitle: dayTitles[1]
          })
        }
      }).catch(() => hide())
  }

  componentDidMount () {
    this.getCourt()
  }

  // 渲染组庭列表
  renderCourtList () {
    const { todayCourtList, nextDayCourtList, activeCourtTag, nextDayTitle } = this.state
    let list = []
    if (activeCourtTag === nextDayTitle) {
      list = nextDayCourtList
    } else {
      list = todayCourtList
    }
    if (list.length === 0) {
      return <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
    }
    return (
      <div className={style['court-list']}>
        {list.map(li => {
          return (
            <div className={style['court-line']} key={li.id}>
              <div className={style['court-line__time']}>{this.getTime(li.begin_datetime)}</div>
              <div>
                <div>庭室：{li.name}{li.hearingroom_remark ? li.hearingroom_remark : ''}</div>
                <div>{li.caseno_desc}  {li.operator}</div>
                <div>仲裁员：{this.getArbitrator(li)}</div>
              </div>
            </div>
          )
        })}
      </div>
    )
  }

  render () {
    return (
      <div>
        <div className={style['block-title']}>
          <div className={style.title}>开庭信息</div>
          <Radio.Group value={this.state.activeCourtTag} onChange={e => this.setState({ activeCourtTag: e.target.value })} size='small'>
            <Radio.Button value='今天'>今天</Radio.Button>
            <Radio.Button value={this.state.nextDayTitle}>{this.state.nextDayTitle}</Radio.Button>
          </Radio.Group>
        </div>
        {this.renderCourtList()}
      </div>
    )
  }
}

const mapState = mapStateToProps(function (state) {
  return {
    uncompleteNum: state.basic.uncompleteNum
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(CourtList)
